"""
对于outer()来说，logo是临时变量，对inner()来说，logo是"常量",可以通过nonlocal关键字进行修改
"""


def outer(logo):
    def inner(mes):
        nonlocal logo
        logo += "*"
        print(f"<{logo}>{mes}<{logo}>")

    return inner


# ATM实例
# 使用闭包实现ATM小案例，initial_amount不会被篡改
def account_create(initial_amount=0):
    def atm(num, deposit=True):
        nonlocal initial_amount
        if deposit:
            initial_amount += num
            print(f"存款：+{num}， 账户余额：{initial_amount}")
        else:
            initial_amount -= num
            print(f"取款：-{num}， 账户余额：{initial_amount}")

    return atm


if __name__ == '__main__':
    fn1 = outer("王欣淼")
    fn1("学习")
    fn1("吃饭")
    fn2 = outer("张三")
    fn2("吃饭")
    atm = account_create()
    atm(100)
    atm(200)
    atm(100, deposit=False)
